package com.spica27.keyboarddemo;

import android.graphics.Insets;
import android.os.Bundle;
import android.view.View;
import android.view.WindowInsets;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

  private RecyclerView rvChat;

  private LinearLayout container;//根视图

  private LinearLayout messageHolder;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
  }

  private void initView() {
    container = findViewById(R.id.container);
    messageHolder = findViewById(R.id.message_holder);
    //初始化RecycleView
    rvChat = findViewById(R.id.rv_chat);
    rvChat.setAdapter(new RecycleviewAdapter());

    //必要，否正inset填充物不生效
    getWindow().setDecorFitsSystemWindows(false);

    container.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
      //OnApplyWindowInsetsListener会自动更新我们的根视图向上移动，根据输入法和底部虚拟导航栏的size
      @Override public WindowInsets onApplyWindowInsets(View rootView, WindowInsets windowInsets) {
        Insets barsIme = windowInsets.getInsets(WindowInsets.Type.systemBars() | WindowInsets.Type.ime());
        rootView.setPadding(barsIme.left, barsIme.top, barsIme.right, barsIme.bottom);
        //这里返回一个新的设置替代WindowInsets.consumeSystemWindowInsets()
        return WindowInsets.CONSUMED;
      }
    });

    //TranslateViewInsetsAnimationListener会自动它会自动移动他的绑定视图作为软键盘动画
    rvChat.setWindowInsetsAnimationCallback
        (new TranslateViewInsetsAnimationListener(rvChat, WindowInsets.Type.ime()));

    messageHolder.setWindowInsetsAnimationCallback
        (new TranslateViewInsetsAnimationListener(messageHolder, WindowInsets.Type.ime()));
    //通过监听用户对RecycleView滚动监听来控制拖动打开IME（软键盘）
    rvChat.setOnTouchListener(new InsetsAnimationOverscrollingTouchListener());

  }

}
